Mask = function(options){
	var _options = {};
	$.extend(_options,this._options,options);
	$.extend(this,{
		_options:_options
	});
	this._init();
};
Mask.prototype = {
	_options:{
		isEsc:true,
		target:window.document.body
	},
	_target:null,
	_content:null,
	_element:null,
	_mask:null,
	_callbacks:[],
	_isCenter:false,
	_init:function(){
		this._target = this._options.target;
		this._target.style.position = "relative";
		this._mask = document.createElement("div");
		this._content = document.createElement("div");
		
		this._mask.style.position = "absolute";
		this._mask.style.top = "0px";
		this._mask.style.left = "0px";
		this._mask.style.display = "none";
		this._mask.style.textAlign = "center";
		this._mask.style.background = "rgba(100,100,100,0.5)";
		

		this._mask.appendChild(this._content);
		this._target.appendChild(this._mask);
	},
	_resize:function(){
		//清零位置防止滚动条
		this._mask.style.width="0px";
		this._mask.style.height = "0px";
		
		//重新设定位置
		var height = this._target.scrollHeight;
		var width = this._target.scrollWidth;
		this._mask.style.width = Utils.pixel.style(width);
		this._mask.style.height = Utils.pixel.style(height);
		
		//重设内容位置
		this._resetContentPosition();
	},
	_resetContent:function(){
		this._content.innerHTML="";
		this._content.appendChild(this._element);
		this._content.style.width = "100%";
		this._content.style.height = "100%";
		
		this._resetContentPosition();
	},
	_resetContentPosition:function(){
		//清除固定重新计算位置
		this._element.style.position="";
		var elementWidth = parseInt(this._element.style.width);
		var elementHeight = parseInt(this._element.style.height);
		
		var offsetWidth = this._target.offsetWidth;
		var offsetHeight = this._target.offsetHeight;
		
		var left = offsetWidth - (offsetWidth/2+elementWidth/2);
		var top = offsetHeight - (offsetHeight/2+elementHeight/2);
		if(!this._isCenter){
			top -= elementHeight*1/7;
		}
		this._element.style.position = "absolute";
		this._element.style.left = Utils.pixel.style(left);
		this._element.style.top = top<0?0:Utils.pixel.style(top);
		
		//重新渲染后再固定
		this._element.style.position="fixed";
	},
	_listen:function(window){
		var frame = null;
		for(var i=0;i<window.frames.length;i++){
			frame = window.frames[i];
			this._listen(frame);
		}
		$(window).bind("keyup",$.proxy(this._onEscKeypress,this));
	},
	_unlisten:function(window){
		$(window).unbind("keyup");
	},
	_onEscKeypress:function(event){
		if(event.keyCode && event.keyCode == 27){
			this._unlisten();
			this.close();
		}
	},
	setTarget:function(target){
		this._target = target;
		this._target.appendChild(this._mask);
	},
	open:function(element,isCenter){
		$(window).bind("resize",$.proxy(this._resize,this));
		if(isCenter){
			this._isCenter = isCenter;
		}
		this._element=element;
		this._resetContent();
		this._resize();
		this._mask.style.display = "block";
		if(this._options.isEsc) this._listen(window.top);
	},
	close:function(callback){
		if(callback && typeof(callback)=="function"){
			this._callbacks.push(callback);
			return;
		}
		this._mask.style.display = "none";
		for(var i=0;i<this._callbacks.length;i++){
			this._callbacks[i]();
		}
	}
};